<!-- views/counselor/Home.vue -->
<template>
  <el-container class="main-container">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="header-left">
        <h1>黔南民族师范学院计信学院学生去向登记系统</h1>
      </div>
      <div class="header-right">
        <span class="user-info">欢迎，{{ $userStore.name }}-老师</span>
        <el-button size="small" @click="logout">退出</el-button>
      </div>
    </el-header>

    <el-container>
      <!-- 左侧菜单 -->
      <el-aside width="220px" class="sidebar">
        <el-menu
            :default-active="activeMenu"
            background-color="#304156"
            text-color="#bfcbd9"
            active-text-color="#409EFF"
        >
          <!-- 班级管理 -->
          <el-sub-menu index="1">
            <template #title>
              <el-icon><Menu /></el-icon>
              <span>班级管理</span>
            </template>
            <template v-for="grade in classTree" :key="grade.grade">
              <el-sub-menu
                  :index="'grade-' + grade.grade"
                  v-if="grade.classes && grade.classes.length > 0"
              >
                <template #title>{{ grade.grade }}级</template>
                <el-menu-item
                    v-for="cls in grade.classes"
                    :key="cls.id"
                    @click="selectClass(cls)"
                >
                  {{ cls.className }}
                </el-menu-item>
              </el-sub-menu>
            </template>
          </el-sub-menu>

          <!-- 假期管理 -->
          <el-sub-menu index="2">
            <template #title>
              <el-icon><Calendar /></el-icon>
              <span>节假日管理</span>
            </template>
            <el-menu-item @click="$router.push('/counselor/holidays')">
              <el-icon><Edit /></el-icon>
              <span>节假日发布</span>
            </el-menu-item>
            <el-menu-item @click="$router.push('/counselor/report')">
              <el-icon><DataAnalysis /></el-icon>
              <span>报表统计</span>
            </el-menu-item>
          </el-sub-menu>

          <!-- 个人信息 -->
          <el-menu-item index="3" @click="$router.push('/counselor/profile')">
            <el-icon><User /></el-icon>
            <span>个人信息管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 右侧内容区 -->
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      activeMenu: '1',
      classTree: []
    };
  },
  async mounted() {
    await this.loadClassTree();
  },
  methods: {
    /**
     * 加载班级树
     */
    async loadClassTree() {
      try {
        this.classTree = await $counselorAPI.getClassTree();
      } catch (err) {
        console.error('加载班级失败', err);
      }
    },

    /**
     * 选择班级
     *
     * @param cls - 班级对象
     */
    selectClass(cls) {
      this.$router.push(`/counselor/class/${cls.id}`);
    },

    /**
     * 退出登录
     */
    logout() {
      this.$confirm('确定要退出登录吗？', '退出系统', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        $userStore.logout();
        this.$router.push('/login');
      });
    }
  }
};
</script>
